package com.systemsjr.jrenterprise.user;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.systemsjr.jrenterprise.user.vo.UserDetailsVO;
import com.systemsjr.jrenterprise.utils.ServiceUtils;
import com.systemsjr.jrenterprise.utils.UIUtils;
import com.systemsjr.jrlib.richclient.BaseItemControl;

public class UserControl extends BaseItemControl<UserDetailsVO> {
	
	public UserControl(){
		super();
		valueHolder = UIUtils.getUserValueHolder();
	}

	@Override
	public void handleDeleteItem(UserDetailsVO item) {
		ServiceUtils.getUserService().delete(item);
	}

	@Override
	public UserDetailsVO handleNewItem() {
		return ServiceUtils.createDefaultUser();
	}

	@Override
	public UserDetailsVO handleSaveItem(UserDetailsVO item) {
		setAction(item.getId());
		BCryptPasswordEncoder encoder = (BCryptPasswordEncoder) getApplicationContext().getBean("passwordEncoder");
		if(item.getPassword1() != null){
			item.setPassword(encoder.encode(item.getPassword1()));
		}
		item = ServiceUtils.getUserService().save(item);
		
		return item;
	}


}
